//
//  HWtianjiangxianxiangController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/3/6.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWtianjiangxianxiangController.h"

@interface HWtianjiangxianxiangController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *yonghuTextField;
@property (weak, nonatomic) IBOutlet UITextField *mimaTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFeild;
@property (weak, nonatomic) IBOutlet UIView *fenchengView;
@property (weak, nonatomic) IBOutlet UIView *shifouyuxungdouluBut;

@property(strong,nonatomic)UIButton *allowBut;
@property(strong,nonatomic)UIButton *RefusedBut;
@property(strong,nonatomic)NSString *yongxunStr; //1 允许   2.登录

@property(strong,nonatomic) NSArray *shujuArr; // 查询的数据
@property(strong,nonatomic) NSMutableArray *storageButMuArr;
@property(strong,nonatomic) NSDictionary *xuanzhongDic; // 选中的等级


@end

@implementation HWtianjiangxianxiangController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    // 设置常用的属性
    [self setCommonProperties];
    
    
    
    // 添加下线
    [self setupUI];
    
    [self sendNetworkRequest];
    

    
}


- (void)setCommonProperties {
    
    self.yongxunStr = @"1"; 
    self.navigationItem.title = @"添加下线";
    self.view.backgroundColor = beijingyanSe;
    self.yonghuTextField.delegate = self;
     self.mimaTextFeild.delegate = self;
    self.phoneTextFeild.delegate = self;
    
    
     self.mimaTextFeild.keyboardType = UIKeyboardTypeASCIICapable;
     self.phoneTextFeild.keyboardType = UIKeyboardTypePhonePad;
    
    
    
    
    
   
    
    
}

//设置UI
- (void)setupUI {
    
    
   //  self.allowBut =
    

          UIButton *submitbutton = [UIButton buttonWithType:UIButtonTypeCustom];
          submitbutton.frame = CGRectMake(20, 20,55,32);
          
          [submitbutton setTitle:@"允许" forState:UIControlStateNormal];
          [submitbutton setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
          submitbutton.titleLabel.font = [UIFont systemFontOfSize:14];
          [submitbutton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
          submitbutton.backgroundColor = RGB(85, 89, 234);
          submitbutton.layer.cornerRadius = 4;
          submitbutton.layer.masksToBounds = YES;
          self.allowBut = submitbutton;
          [self.shifouyuxungdouluBut addSubview:submitbutton];
    
    
         UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
          submitbutton1.frame = CGRectMake(submitbutton.right+10 , 20,55,32);
          
          [submitbutton1 setTitle:@"拒绝" forState:UIControlStateNormal];
          [submitbutton1 setTitleColor:RGB(193, 193, 203) forState:UIControlStateNormal];
          submitbutton1.titleLabel.font = [UIFont systemFontOfSize:14];
          [submitbutton1 addTarget:self action:@selector(submitap) forControlEvents:UIControlEventTouchUpInside];
          submitbutton1.backgroundColor = RGB(245, 245, 252);
          submitbutton1.layer.cornerRadius = 4;
          submitbutton1.layer.masksToBounds = YES;
          self.RefusedBut = submitbutton1;
          [self.shifouyuxungdouluBut addSubview:submitbutton1];
    
    
    
    
    
}





// 发送网络请求
- (void)sendNetworkRequest {
    
    
    
     
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *uid =  [HWLocallyStoredData getLoginUserID];
         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
         NSDictionary *parametes = @{      @"ios" : @"1",
                                      @"ct" : @"app",
                                        @"ac" : @"down_fcbl",
                                        @"t" : tStr,
                                        @"uid":uid,
                                        @"sign" :signStrMd5
                                        };
        
        
            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                  
                 

                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                  if ([codeStr intValue] == 1) {
                 
                         NSLog(@"%@",response);
                    
                      self.shujuArr = (NSArray *)response[@"data"];
                      
                      [self addkongjian];
                      
                      
                      
                      
                      
                       
                  } else {
                      
                    [MBProgressHUD showError:response[@"message"]];
                                    
                
                      
                  }
                  
             
                  
              } failure:^(NSString *error) {
                 // [SVProgressHUD dismiss];
                  
                   [MBProgressHUD showSuccess:@"登录失败"];
                  [MBProgressHUD hideHUD];
                  
                  
              }];
        
    
    
    
    
    
    
    
}


//添加控件
- (void)addkongjian {
    self.storageButMuArr = [[NSMutableArray alloc]init];
    NSInteger count =  self.shujuArr.count;
    
    for (int i = 0 ; i < count; i++) {
        
            NSDictionary  *dic = self.shujuArr[i];
        
        
              UIButton *submitbutton = [UIButton buttonWithType:UIButtonTypeCustom];
              submitbutton.frame = CGRectMake(20+i*10+i*55 , 20,55,32);
              
              [submitbutton setTitle:dic[@"title"] forState:UIControlStateNormal];
              [submitbutton setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
              submitbutton.titleLabel.font = [UIFont systemFontOfSize:14];
              [submitbutton addTarget:self action:@selector(xuanzedengji:) forControlEvents:UIControlEventTouchUpInside];
              submitbutton.backgroundColor = RGB(85, 89, 234);
              submitbutton.layer.cornerRadius = 4;
              submitbutton.layer.masksToBounds = YES;
              submitbutton.tag = i;
              [self.fenchengView addSubview:submitbutton];
              [self.storageButMuArr addObject:submitbutton];
           
        if (i == 0) {
             NSDictionary  *dic1 = self.shujuArr[i];
             self.xuanzhongDic = dic1;
        } else {
            
            submitbutton.backgroundColor =  RGB(245, 245, 252);
            [submitbutton setTitleColor:RGB(193, 193 , 203) forState:UIControlStateNormal];
            
        }
           
        
        
    }
    
    
    
    
    



}




// 选中tag
-(void)xuanzedengji:(UIButton *)sender {
    
    
    
    
     NSInteger tag = sender.tag;
     self.xuanzhongDic = self.shujuArr[tag];
     for (int i = 0 ; i < self.storageButMuArr.count; i++) {
        UIButton *butt = self.storageButMuArr[i];
        
        
        butt.backgroundColor =  RGB(245, 245, 252);
        [butt setTitleColor:RGB(193, 193 , 203) forState:UIControlStateNormal];
                 
    }
    
    
     sender.backgroundColor = RGB(85, 89, 234);
     [sender setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
   

    
    
    
}










// 一键登录
- (IBAction)yijianBut:(UIButton *)sender {
    
   
    
    if (!(self.yonghuTextField.text.length > 0)) {
         [MBProgressHUD showError:@"用户名不能为空"];
          return;
        
    } else if (!(self.mimaTextFeild.text.length > 0)){
        
        [MBProgressHUD showError:@"密码不能为空"];
         return;
        
        
    }else if ([WMUtil isBlank:self.mimaTextFeild.text]) {
        
        [MBProgressHUD showError:@"密码不能有空格"];
        return;
        
    }
    else if (!(self.phoneTextFeild.text.length > 0)){
        
        [MBProgressHUD showError:@"电话不能为空"];
        return;
    
    } else if (![WMUtil validateMobile:self.phoneTextFeild.text]){
        
        [MBProgressHUD showError:@"请输入正确的电话号码"];
        
        return;
    
    }
    
    
    if (self.xuanzhongDic == nil) {
        
         [MBProgressHUD showError:@"请选择等级"];
               return;
    }
    
    
    
    
          NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *username = self.yonghuTextField.text;
          NSString *passwd = self.mimaTextFeild.text;
          NSString *phone = self.phoneTextFeild.text;
          NSString *fcbl =  [NSString stringWithFormat:@"%@",self.xuanzhongDic[@"fcbl"]];
         NSString *is_login  = self.yongxunStr;
    
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",uid,username,passwd,phone,fcbl,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
    
        [MBProgressHUD showMessage:@"一键添加"];
          NSDictionary *parametes = @{      @"ios" : @"1",
                                       @"ct" : @"app",
                                         @"ac" : @"down_add",
                                         @"t" : tStr,
                                         @"uid":uid,
                                         @"username":username,
                                          @"passwd":passwd,
                                          @"phone":phone,
                                            @"fcbl" : fcbl,
                                            @"is_login":is_login,
                                         @"sign" :signStrMd5
                                         };
         
         
             [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                   
                  

                   NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                 
                    [MBProgressHUD hideHUD];  
                   if ([codeStr intValue] == 1) {
                  
                          NSLog(@"%@",response);
                       [self.navigationController popViewControllerAnimated:YES];
                       
                       
                       
                       
                       
                        
                   } else {
                       
                       [MBProgressHUD showError:response[@"message"]];
                                     
                 
                       
                   }
                   
              
                   
               } failure:^(NSString *error) {
                  // [SVProgressHUD dismiss];
                   
                    [MBProgressHUD showSuccess:@"登录失败"];
                   [MBProgressHUD hideHUD];
                   
                   
               }];
    
    
    
    
    
    
    
}










- (void)submit {
    
    self.yongxunStr = @"1";
    self.allowBut.backgroundColor = RGB(85, 89, 234);
    self.RefusedBut.backgroundColor = RGB(245, 245, 252);
    
    [self.allowBut setTitleColor:RGB(255, 255, 255)  forState:UIControlStateNormal];
    [self.RefusedBut setTitleColor:RGB(193, 193, 203) forState:UIControlStateNormal];
    
    
    
    
}

- (void)submitap {
      self.yongxunStr = @"2";
      self.allowBut.backgroundColor =  RGB(245, 245, 252);
      self.RefusedBut.backgroundColor =  RGB(85, 89, 234);
   
      [self.allowBut setTitleColor:RGB(193, 193, 203) forState:UIControlStateNormal]; 
     [self.RefusedBut setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    
    [textField resignFirstResponder];
    return  YES;
}

@end
